package com.cuit.riskassessment.action.area;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cuit.riskassessment.service.AreaService;
import com.cuit.riskassessment.util.Result;
import com.cuit.riskassessment.util.Tree;
import com.cuit.riskassessment.vo.Area;

/**
 * Area 操作的相关action
 * @author Yang_Zhiqin-pc
 *
 */
@Controller
@RequestMapping(value="area")
public class AreaAction {
	@Autowired
	@Resource(name="areaService")
	private AreaService areaService;
	
	@RequestMapping(value="/trees")
	public @ResponseBody Object treeInfoJson(HttpServletResponse response,@RequestParam(value="id") Integer id)
	{
		response.setHeader("Accept",  "application/json");
		return this.areaService.getAreas(id);
	}
	
	
	@RequestMapping(value="/addarea")
	public  @ResponseBody Object addArea(HttpServletResponse response ,Area area)
	{
		response.setHeader("Accept",  "application/json");
		String message =new String("操作成功！");
		
		Result res = new Result();
		res.setSuccess(true);
		if(area.getId()==null){
			//添加新的东西
			if(!this.areaService.addArea(area))
			{
				message =new String("添加失败！");
				res.setSuccess(false);
			}
		}else
		{
			//更新信息
			if(!this.areaService.updateArea(area))
			{
				message =new String("更新失败！");
				res.setSuccess(false);
			}
			
		}
		res.setMessage(message);
		return  res;
	}
	
	@RequestMapping(value="/del")
	public @ResponseBody Object delArea(HttpServletResponse response,@RequestParam(value="id") Integer id)
	{
		response.setHeader("Accept",  "application/json");
		
		String message =new String("删除成功！");
		Result res = new Result();
		res.setSuccess(true);
		if(!this.areaService.delArea(id))
		{
			message =new String("删除失败！");
			res.setSuccess(false);
		}
		res.setMessage(message);
		return  res;
		//return this.areaService.getAreas(id);
	}
	
	@RequestMapping(value="/getInfo")
	public @ResponseBody Object getInfo(HttpServletResponse response,@RequestParam(value="id") Integer id)
	{
		response.setHeader("Accept",  "application/json");
		Result res = new Result();
		Tree obj = (Tree) this.areaService.getAreaById(id);
		Tree  up =null;
		Map<String,Object> result = new HashMap<String,Object>();
		if(obj==null)
		{
			res.setSuccess(false);
		}else
		{
			up = (Tree) this.areaService.getAreaById(obj.getParentId());
			result.put("areaname", obj.getText());
			
			if(up!=null)
					result.put("upname", up.getText());
			else 
				    result.put("upname", "顶级目录");
			
			result.put("count", this.areaService.getAreaCount(id));
			res.setSuccess(true);
			res.setOthers(result);
		}
		return  res;
	}
	
	
	@RequestMapping(value="/detail")
	public @ResponseBody Object detail(HttpServletResponse response,@RequestParam(value="id") Integer id)
	{
		response.setHeader("Accept",  "application/json");
		return  this.areaService.getDetail(id);
	}
	
}
